#pragma once
#include "KeyboardInput.h"
#include "MouseInput.h"
#include "Actor.h"
#include "util.h"
//#include "ModelController.h"

namespace banknamespace
{
	class InputController
	{
	public:
		static void setMouseCenter(HWND hwnd)
		{
			RECT cRect,wRect;
			GetClientRect( hwnd, &cRect );
			GetWindowRect(hwnd, &wRect);
			int border_thickness = (wRect.right - wRect.left - cRect.right) /2;

			MOUSE_CENTER_X = cRect.right / 2 + border_thickness + wRect.left; 
			MOUSE_CENTER_Y = (cRect.bottom - cRect.top +45) / 2 + wRect.top;
			 
			::SetCursorPos(MOUSE_CENTER_X,MOUSE_CENTER_Y);
		}

		static bool didMouseDown(unsigned int mouseButton)
		{
			Mouse& mouse = GetTheMouse();
			return mouse.didGoDown(mouseButton);
		}
		static void update(Actor& camera, Actor& model, vec3f& lightPos, vec3f& lightPos2, Actor& stonecam/*, Actor& spincube*/)
		{
			//bool updateFrustum = true;
			//updateCamCluster = true;
			Mouse& mouse = GetTheMouse();
			Keyboard& keyboard = GetTheKeyboard();

			const float KEYBOARD_IMPULSE_SCALAR = 0.1f;
			vec3f cameraImpulse(0,0,0);

			if ( keyboard.isDown ('W') )
			{
				cameraImpulse.z -= KEYBOARD_IMPULSE_SCALAR;
			}
			if ( keyboard.isDown ('S') )
			{
				cameraImpulse.z += KEYBOARD_IMPULSE_SCALAR;
			}
			if ( keyboard.isDown ('A') )
			{
				cameraImpulse.x -= KEYBOARD_IMPULSE_SCALAR;
			}
			if ( keyboard.isDown ('D') )
			{
				cameraImpulse.x += KEYBOARD_IMPULSE_SCALAR;
			}
			if ( keyboard.isDown ( VK_SPACE ) )
			{
				cameraImpulse.y += KEYBOARD_IMPULSE_SCALAR;
			}
			if ( keyboard.isDown ( VK_CONTROL ) || IsKeyDown ( 'C' ) )
			{
				cameraImpulse.y -= KEYBOARD_IMPULSE_SCALAR;
			}

			if ( mouse.isDown ( 1 ) )
			{
				lightPos2 = camera.position();
			}
			

			// rotate the camera
// 			RECT screenRect;
// 			GetWindowRect( GetDesktopWindow(), &screenRect );
// 			int MOUSE_CENTER_X = screenRect.right / 2;
// 			int MOUSE_CENTER_Y = screenRect.bottom / 2;
			
			POINT mousePos;
			::GetCursorPos( &mousePos );

			// how far did the mouse move?
			// 
			const float MOUSE_ROTATION_SCALAR = 0.03f;
			vec3f mouseDelta( MOUSE_ROTATION_SCALAR * static_cast<float>( -mousePos.y + MOUSE_CENTER_Y )
				, MOUSE_ROTATION_SCALAR * static_cast<float>( -mousePos.x + MOUSE_CENTER_X ) );

			//camera.rotate( mouseDelta );
			if ( keyboard.isDown('E') )
			{
				stonecam.applyAngularImpulse( mouseDelta );
				stonecam.applyImpulse(cameraImpulse);
			}
			else if ( keyboard.isDown('R') )
			{
				//spincube.applyAngularMomentum( cameraImpulse*100.0f );
			}
			else
			{
				camera.applyAngularImpulse( mouseDelta );
				camera.applyImpulse(cameraImpulse);
			}
			
			//player.rotate( mouseDelta );

			// re-center the mouse
			::SetCursorPos( MOUSE_CENTER_X, MOUSE_CENTER_Y );

			// matrix
			//float rotationMat[9];
			
			//fromEulerAngles( (camera.rotation().x) * (3.1416f / 180.0f), (camera.rotation().y) * (3.1416f / 180.0f), (camera.rotation().z) * (3.1416f / 180.0f), rotationMat );
			//vec3f worldSpaceImpulse = transform( rotationMat, cameraImpulse );

			//camera.applyImpulse(worldSpaceImpulse);
			//camera.applyImpulse(cameraImpulse);
			//player.applyImpulse(worldSpaceImpulse);

		}
	private:
		static LONG MOUSE_CENTER_X ;
		static LONG MOUSE_CENTER_Y ;
		
	};	
}
